package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.ColorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.ColorForm;
import com.youlai.boot.factory.model.query.ColorQuery;
import com.youlai.boot.factory.model.vo.ColorVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 系统颜色库前端控制层
 *
 * @author youlaitech
 * @since 2025-07-06 16:08
 */
@Tag(name = "系统颜色库接口")
@RestController
@RequestMapping("/api/v1/color")
@RequiredArgsConstructor
public class ColorController  {

    private final ColorService colorService;

    @Operation(summary = "系统颜色库分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:color:query')")
    public PageResult<ColorVO> getColorPage(ColorQuery queryParams ) {
        IPage<ColorVO> result = colorService.getColorPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增系统颜色库")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:color:add')")
    public Result<Void> saveColor(@RequestBody @Valid ColorForm formData ) {
        boolean result = colorService.saveColor(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取系统颜色库表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:color:edit')")
    public Result<ColorForm> getColorForm(
        @Parameter(description = "系统颜色库ID") @PathVariable Long id
    ) {
        ColorForm formData = colorService.getColorFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改系统颜色库")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:color:edit')")
    public Result<Void> updateColor(
            @Parameter(description = "系统颜色库ID") @PathVariable Long id,
            @RequestBody @Validated ColorForm formData
    ) {
        boolean result = colorService.updateColor(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除系统颜色库")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:color:delete')")
    public Result<Void> deleteColors(
        @Parameter(description = "系统颜色库ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = colorService.deleteColors(ids);
        return Result.judge(result);
    }
}
